package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("add")
    public Result add(@RequestParam("permissionIds") Integer[] permissionIds, @RequestParam("menuIds") Integer[] menuIds, @RequestBody Role role) {

        try {
            roleService.add(permissionIds, menuIds, role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }
    @RequestMapping("findAll")
    public Result findAll() {

        try {
            List<Role> list = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    /**
     * 角色信息分页查询
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        try {
            PageResult pageResult = roleService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }

    /**
     * 通过id查询检查组信息
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id") Integer id) {

        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 执行编辑更新
     */
    @RequestMapping("edit")
    public Result edit(@RequestParam("permissionIds")Integer[] permissionIds, @RequestParam("menuIds")Integer[] menuIds, @RequestBody Role role) {

        try {
            roleService.edit(permissionIds,menuIds,role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }

    }

    /**
     * 通过id删除检查组
     */
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id") Integer id) {

        try {
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }

    }

    /**
     * 查询所有检查组信息
     */
    @RequestMapping("findPermission")
    public Result findPermission() {

        try {
            List<Permission> list = roleService.findPermission();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    @RequestMapping("findMenu")
    public Result findMenu() {

        try {
            List<Menu> list = roleService.findMenu();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(@RequestParam("id") Integer id) {

        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(@RequestParam("id") Integer id) {

        try {
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("findRoleIdsByUserId")
    public Result findRoleIdsByUserId(@RequestParam("id")Integer id){

        try {
            List<Integer> roleIds=roleService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}